package com.lovo.service.impl;

import com.lovo.dao.StudentDAO;
import com.lovo.dto.StudentDTO;
import com.lovo.dto.StudentPageDTO;
import com.lovo.enums.StudentCodeEnum;
import com.lovo.exception.StudentException;
import com.lovo.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @title
 * @author: luo
 * @date: 2020/11/20 21:02
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDAO studentDAO;

    @Override
    public void addStudent(StudentDTO studentDTO) {
        if (studentDAO.addStudent(studentDTO) <= 0) {
            throw new StudentException(StudentCodeEnum.REGISTER_FAIL);
        }
    }

    @Override
    public List<StudentDTO> findAll(StudentPageDTO pageDTO) {
        List<StudentDTO> list = studentDAO.selectAll(pageDTO);
        if (list == null || list.size()<=0) {
            throw new StudentException(StudentCodeEnum.NO_DATA);
        }
        return list;
    }

    @Override
    public void deleteStudent(Integer studentId) {
        int i = studentDAO.deleteStudent(studentId);
        if (i<=0) {
            throw new StudentException(StudentCodeEnum.FAIL_DELETE_USER);
        }
    }

    @Override
    public void updateStudent(StudentDTO studentDTO) {
        int i = studentDAO.updateStudent(studentDTO);
        if (i<=0) {
            throw new StudentException(StudentCodeEnum.FAIL_UPDATE_USER);
        }
    }

    @Override
    public StudentDTO findStudentById(Integer studentId) {
        StudentDTO student = studentDAO.selectStudentById(studentId);
        if (student == null) {
            throw new StudentException(StudentCodeEnum.NO_DATA);
        }
        return student;
    }

//    @Override
//    public List<StudentDTO> findAllStudents() {
//        return studentDAO.;
//    }
}
